package com.example.easyexcel.exports.custom_export.CustomFormulation.strategy;

import cn.hutool.json.JSONUtil;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.AbstractCellStyleStrategy;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.util.List;

public class ColumnStyleStrategy extends AbstractCellStyleStrategy {
    private static List<Integer> titleRowIndex;
    private static WriteCellStyle contentCellStyle;
    private static WriteCellStyle titleCellStyle;

    static {
        // 字体样式
        WriteFont contentWriteFont = new WriteFont();
        //contentWriteFont.setColor(IndexedColors.BLACK.getIndex());
        //contentWriteFont.setBold(false);
        //contentWriteFont.setFontName("微软雅黑");
        //contentWriteFont.setFontHeightInPoints((short) 10);
        //内容样式
        contentCellStyle = new WriteCellStyle();
        contentCellStyle.setWriteFont(contentWriteFont);
        contentCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentCellStyle.setBorderLeft(BorderStyle.THIN);
        contentCellStyle.setBorderRight(BorderStyle.THIN);
        contentCellStyle.setBorderTop(BorderStyle.THIN);
        contentCellStyle.setBorderBottom(BorderStyle.THIN);
        contentCellStyle.setShrinkToFit(false);
        contentCellStyle.setWrapped(false);
        //contentCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        //contentCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        // 标题样式
        titleCellStyle = JSONUtil.parse(contentCellStyle).toBean(WriteCellStyle.class);
        titleCellStyle.getWriteFont().setColor(IndexedColors.BLACK.getIndex());
        titleCellStyle.getWriteFont().setBold(true);
        titleCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        titleCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
    }

    public ColumnStyleStrategy(List<Integer> titleRowIndex) {
        this.titleRowIndex = titleRowIndex;
    }

    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        WriteCellStyle cellStyle;
        if (titleRowIndex.contains(context.getRowIndex())) {
            cellStyle = titleCellStyle;
        } else {
            cellStyle = contentCellStyle;
        }
        context.getCell().setCellStyle(context.getWriteWorkbookHolder().createCellStyle(cellStyle, context.getCell().getCellStyle()));
    }

}




